import { PrismaService } from '@/common/service/prisma/prisma.service';
import { Processor, Process } from '@nestjs/bull';
import { Job } from 'bull';
@Processor('autoCheckIfItExpires')
export class examineProcessor {
    constructor(private readonly prisma: PrismaService) {}
    @Process()
    async handleLeaseExpiration(job: Job) {
        const { rentalLeaseId } = job.data;
        const lease = await this.prisma.rentalLease.findUnique({
            where: { id: rentalLeaseId },
        });
        if (new Date(lease.endDate) <= new Date() && lease.leaseStatus === '0') {
            // 租赁到期，更新租赁关系状态为"结束"
            this.prisma.$transaction([
                this.prisma.rentalLease.update({
                    where: { id: rentalLeaseId },
                    data: { leaseStatus: '1' },
                }),
                this.prisma.rentalHouse.update({
                    where: { id: lease.houseId },
                    data: { status: '1' },
                })
            ])
        }
    }
}
